﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace debugger
{
    public partial class Form4 : Form
    {

        public Form4()
        {
            InitializeComponent();
        }
        public Form1 mainForm;

        byte[] data_bmp = new byte[2048 * 768];

        bool reset = false;

        public void getbmp()
        {
            bool fast341 = checkBox3.Checked;

            uint baseAddr = uint.Parse(this.mainForm.textBox7.Text) / 2 * 2048;// Convert.ToUInt32(this.textBox1.Text, 16);

            Stopwatch sw = new Stopwatch();
            sw.Start();
            mainForm.getVGA(data_bmp, baseAddr, fast341, reset);
            sw.Stop();
            long time = sw.ElapsedMilliseconds;
            double speed = 1.5 / time * 1000;
            //Console.WriteLine(speed);

            reset = false;

            Bitmap bmp = new Bitmap(1024, 768, PixelFormat.Format16bppRgb565);
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = 2048 * 768;
            System.Runtime.InteropServices.Marshal.Copy(data_bmp, 0, ptr, bytes);
            bmp.UnlockBits(bmpData);

            this.pictureBox1.Image = bmp;
        }

        System.Timers.Timer t = new System.Timers.Timer();
        private void Form4_Load(object sender, EventArgs e)
        {
            this.pictureBox1.MouseDown += pictureBox1_MouseDown;
            this.pictureBox1.MouseMove += pictureBox1_MouseMove;
            this.pictureBox1.MouseUp += pictureBox1_MouseUp;

            //this.KeyDown += Form4_KeyDown;
            this.KeyUp += Form4_KeyUp;

            t.Interval = 1;
            t.Elapsed += t_Elapsed;
            t.Start();
            this.FormClosed += Form4_FormClosed;

            mainForm.videoing = true;
        }
        void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainForm.videoing = false;
            t.Stop();
            t.Dispose();
        }
        class KeyEvent
        {
            public uint key;
        }
        class MouseEvent
        {
            public uint v1;
            public uint v2;
        }

        Queue<KeyEvent> keyEvent = new Queue<KeyEvent>();
        Queue<MouseEvent> mouseEvent = new Queue<MouseEvent>();

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            t.Stop();
            try
            {
                while (true)
                {
                    if (keyEvent.Count <= 0)
                    {
                        break;

                    }
                    KeyEvent ke = keyEvent.Dequeue();
                    Console.WriteLine(Util.getHex8(ke.key));
                    mainForm.sendCmd(Form1.SET_DEBUG_HID, ke.key);// key;
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);
                }

                while (true)
                {
                    if (mouseEvent.Count <= 0)
                    {
                        break;

                    }
                    MouseEvent me = mouseEvent.Dequeue();

                    mainForm.sendCmd(Form1.SET_DEBUG_HID, me.v1);

                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);

                    mainForm.sendCmd(Form1.SET_DEBUG_HID, me.v2);

                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);
                }

                if (checkBox1.Checked)
                {
                    getbmp();
                }
                t.Start();
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
            }

        }
        void Form4_KeyUp(object sender, KeyEventArgs e)
        {
            uint key = Key.getKey(e.KeyData, false, 0);
            KeyEvent qe = new KeyEvent();
            qe.key = key;
            keyEvent.Enqueue(qe);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (msg.Msg == 0x100)
            {
                KeyEventArgs e;
                int LParam = msg.LParam.ToInt32();

                uint key = Key.getKey(keyData, true, LParam);
                {
                    KeyEvent qe = new KeyEvent();
                    qe.key = key;
                    keyEvent.Enqueue(qe);
                }
            }
            return true;            // 返回 true 表示“已处理”，阻止继续传递
        }

        void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            MouseEvent me = new MouseEvent();
            me.v1 = 0x8E000000 | (uint)e.X | btn;
            me.v2 = 0x8F000000 | (uint)e.Y;
            mouseEvent.Enqueue(me);
            btn = 0;
        }

        void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (checkBox2.Checked)
            {
                MouseEvent me = new MouseEvent();
                me.v1 = 0x8E000000 | ((uint)e.X) | btn;
                me.v2 = 0x8F000000 | ((uint)e.Y) | btn;
                mouseEvent.Enqueue(me);
            }
        }

        uint btn;
        void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            btn = ((e.Button == MouseButtons.Left ? 1u : 2u) << 16);

            MouseEvent me = new MouseEvent();
            me.v1 = 0x8E000000 | ((uint)e.X);
            me.v2 = 0x8F000000 | ((uint)e.Y) | btn;
            mouseEvent.Enqueue(me);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                reset = true;
                getbmp();
            }
        }

    }
}
